package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.FarmerCropDetailActivity;
import com.hemaapp.yjnh.listener.OrderItemClickListener;
import com.hemaapp.yjnh.bean.FarmerOrder;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：田家商品订单列表适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/20 17:22
 * 修改备注：
 */
public class FarmerOrderListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<FarmerOrder> datas;
    private Context mContext;
    private OrderItemClickListener clickListener;

    public FarmerOrderListAdapter(Context context, ArrayList<FarmerOrder> orders, OrderItemClickListener clickListener) {
        this.mContext = context;
        this.datas = orders;
        this.clickListener = clickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (datas.size() == 0) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_order, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂时没有订单");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setPosition(position);
                ((ViewHolder) holder).setDatas(datas.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_order_num)
        TextView tvOrderNum;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.layout_goods)
        LinearLayout layoutGoods;
        @Bind(R.id.tv_freight)
        TextView tvFreight;
        @Bind(R.id.tv_total_fee)
        TextView tvTotalFee;
        @Bind(R.id.div_line)
        View divLine;
        @Bind(R.id.tv_cancel)
        TextView tvCancel;
        @Bind(R.id.tv_confirm)
        TextView tvConfirm;
        @Bind(R.id.tv_friend_pay)
        TextView tvFriendPay;
        @Bind(R.id.tv_pay)
        TextView tvPay;
        @Bind(R.id.tv_send)
        TextView tvSend;
        @Bind(R.id.tv_refuse)
        TextView tvRefuse;
        @Bind(R.id.tv_hint)
        TextView tvHint;
        @Bind(R.id.tv_commit)
        TextView tvCommit;
        @Bind(R.id.tv_aftersale)
        TextView tvAftersale;
        @Bind(R.id.layout_bottom)
        LinearLayout layoutBottom;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onItemClickListener(position);
                }
            });
            tvSend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onDeliveryGoods(position);
                }
            });
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public void setDatas(FarmerOrder order) {
            tvOrderNum.setText("订单号：" + order.getBill_sn());
            tvStatus.setText(getTradeStatus(order));
            tvFreight.setText("(含运费¥"+uuUtils.formatAfterDot2(order.getShipping_fee())+")");
            tvTotalFee.setText(uuUtils.formatAfterDot2(order.getTotal_fee()));
            //动态添加多个商品
            if (layoutGoods.getChildCount() != 0) {
                layoutGoods.removeAllViews();
            }
            for (int i = 0; i < order.getChildItems().size(); i++) {
                View view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_order_goods, null);
                ItemViewHolder ivHolder = new ItemViewHolder();
                itemFindView(view, ivHolder);
                setItem(position, this, i, order, view, ivHolder);
                view.setTag(datas.get(position).getChildItems().get(i).getBlog_id());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String blog_id = (String) v.getTag();
                        Intent it = new Intent(mContext, FarmerCropDetailActivity.class);
                        it.putExtra("blog_id", blog_id);
                        mContext.startActivity(it);
                    }
                });
            }
            //根据订单状态来显示下方的操作按钮
            initBottomButton(this);
            setBottomButton(this, order);
        }

        /**获取订单的状态
         * @param bill
         * @return
         */
        private String getTradeStatus(FarmerOrder bill) {
            String status = "";
//			订单状态	1：有效 2：已删除
            int statetype = Integer.parseInt(bill.getStatetype());
            if (statetype == 2) {
                status = "已关闭";
                return status;
            }
            //退货状态	0无, 非0有
            String returnflag = bill.getReturnflag();
            if (!returnflag.equals("0")) {
                return "问题订单";
            }
            //交易状态	0 未付款 1: 已付款待发货 2 待收货 3 已收货待评价 4 已评价交易成功
            int tradetype = Integer.parseInt(bill.getTradetype());
            switch (tradetype) {
                case 0:
                    status = "待付款";
                    break;
                case 1:
                    status = "待发货";
                    break;
                case 2:
                    status = "待收货";
                    break;
                case 3:
                    status = "未评价";
                    break;
                case 4:
                    status = "交易成功";
                    break;
                default:
                    break;
            }
            return status;
        }
    }


    private void initBottomButton(ViewHolder holder) {
        holder.divLine.setVisibility(View.GONE);
        holder.layoutBottom.setVisibility(View.GONE);
        holder.tvSend.setVisibility(View.GONE);
//			holder.tvHint.setVisibility(View.GONE);
    }

    /**根据不同的交易状态显示不同的处理按钮和订单的状态信息
     * @param holder
     */
    private void setBottomButton(ViewHolder holder, FarmerOrder bill) {
        //	交易状态	0 未付款 1: 已付款待发货 2 待收货 3 已收货待评价 4 已评价交易成功
        if (bill.getTradetype().equals("1")) {//1: 已付款待发货
            String itemtype = bill.getChildItems().get(0).getItemtype();
            if (itemtype.equals("0") || itemtype.equals("3")) {//如果没有退货或者退货失败
                holder.divLine.setVisibility(View.VISIBLE);
                holder.layoutBottom.setVisibility(View.VISIBLE);
                holder.tvSend.setVisibility(View.VISIBLE);
            }
        }
    }

    private class ItemViewHolder {
        private ImageView img;
        private TextView name;
        private TextView price;
        private TextView amount;
        private TextView tv_commit;//复用Layout，这里的评论和申请售后，显示为“同意”“拒绝”
        private TextView tv_aftersale;
        private View div;
        private TextView tv_hint;
        private TextView tvGoodsSpec;
    }

    /**一个订单中的多个商品
     * @param view
     * @param ivHolder
     */
    private void itemFindView(View view, ItemViewHolder ivHolder) {
        ivHolder.img = (ImageView) view.findViewById(R.id.img);
        ivHolder.name = (TextView) view.findViewById(R.id.good_name);
        ivHolder.price = (TextView) view.findViewById(R.id.price);
        ivHolder.amount = (TextView) view.findViewById(R.id.count);
        ivHolder.tv_commit = (TextView) view.findViewById(R.id.tv_commit);
        ivHolder.tv_aftersale = (TextView) view.findViewById(R.id.tv_aftersale);
        ivHolder.div = view.findViewById(R.id.divider);
        ivHolder.tv_hint = (TextView) view.findViewById(R.id.tv_hint);
        ivHolder.tvGoodsSpec = (TextView) view.findViewById(R.id.tv_goods_spec);

        ivHolder.tv_commit.setText("同意");
        ivHolder.tv_aftersale.setText("拒绝");
        ivHolder.div = view.findViewById(R.id.divider);
        ivHolder.tv_hint = (TextView) view.findViewById(R.id.tv_hint);
    }

    private void setItem(final int position, ViewHolder holder, int i, final FarmerOrder bill,
                         View view, ItemViewHolder ivHolder) {
        FarmerOrder.ChildItemsBean child = bill.getChildItems().get(i);

        ImageUtils.loadSmalImage(mContext, child.getImgurl(), ivHolder.img);
        ivHolder.name.setText(child.getName());
        ivHolder.price.setText("¥"+uuUtils.formatAfterDot2(child.getPrice()));
        ivHolder.amount.setText("x" + child.getBuycount());
        holder.layoutGoods.addView(view);
        if (i == bill.getChildItems().size()-1) {
            ivHolder.div.setVisibility(View.GONE);
        }else {
            ivHolder.div.setVisibility(View.VISIBLE);
        }
        if (!bill.getReturnflag().equals("0")) {//如果订单有申请售后
            String itemType = child.getItemtype();
            if (itemType.equals("1")) {//状态	0正常 1申请中 2等待买家发货 3拒绝 4退货中 5退货完成
                ivHolder.tv_commit.setVisibility(View.VISIBLE);
                ivHolder.tv_aftersale.setVisibility(View.VISIBLE);
            }else {
                ivHolder.tv_commit.setVisibility(View.GONE);
                ivHolder.tv_aftersale.setVisibility(View.GONE);
                if (itemType.equals("2") || itemType.equals("4") || itemType.equals("5")) {
                    ivHolder.tv_hint.setVisibility(View.VISIBLE);
                    ivHolder.tv_hint.setText("已同意");
                }else if (child.getItemtype().equals("3")) {
                    ivHolder.tv_hint.setVisibility(View.VISIBLE);
                    ivHolder.tv_hint.setText("已拒绝");
                }
            }
        }else {
            ivHolder.tv_commit.setVisibility(View.GONE);
            ivHolder.tv_aftersale.setVisibility(View.GONE);
        }
        ivHolder.tv_commit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickListener != null)
                    clickListener.onAgreeReturn(position);
            }
        });
        ivHolder.tv_aftersale.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickListener != null)
                    clickListener.onRefuseReturn(position);
            }
        });
    }
}
